# 输入年龄页面
# 值：01检查号码
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QLineEdit, qApp, QDesktopWidget, QLabel, QPushButton, QMessageBox
from PyQt5.QtGui import QIcon, QFont,QKeyEvent
from PyQt5.QtCore import Qt


class Age(QMainWindow):
    def __init__(self, jianchahao_value='', age_value=''):
        super().__init__()
        # 传递检查号码
        global jianchahao_text, age_text
        jianchahao_text = jianchahao_value  # 传递来自检查号的页面数据
        age_text = age_value
        self.initUI()

    def initUI(self):
        exitAction = QAction(QIcon('images/exit.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(qApp.quit)

        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAction)

        lbl_jianchahao = QLabel('检查号：' + jianchahao_text, self)
        lbl_jianchahao.setFont(QFont('SansSerif', 16))
        lbl_jianchahao.resize(200, 50)
        lbl_jianchahao.move(50, 0)

        lbl1 = QLabel('年龄\n（0-15）', self)
        lbl1.setFont(QFont('SansSerif', 24))
        lbl1.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        lbl1.resize(100,100)
        lbl1.move(200, 100)

        global line
        line = QLineEdit(self)
        line.setText(age_text)
        line.setFont(QFont('Arial Black', 24))
        line.resize(150, 50)
        line.move(170, 200)

        btn = QPushButton('返回', self)
        btn.setToolTip('<b>上一步</b>')
        btn.setFont(QFont('SansSerif', 14))
        btn.resize(100, 60)
        btn.move(100, 400)
        btn.clicked.connect(self.on_back_clicked)

        btn_next = QPushButton('下一步', self)
        btn_next.setToolTip('<b>下一步</b>')
        btn_next.setFont(QFont('SansSerif', 14))
        btn_next.resize(100, 60)
        btn_next.move(300, 400)
        btn_next.clicked.connect(self.on_next_clicked)

        self.setFixedSize(500, 500)
        self.setWindowTitle('玉环县人民医院 TW3骨龄法 软件')
        self.setWindowIcon(QIcon("images/myicon.png"))
        self.center()
        self.show()

    # 点击返回按钮的时候 引入主界面并将自己销毁
    def on_back_clicked(self):
        from jianchahao import Example
        self.window().destroy()
        self.diag = Example(jianchahao_text)
        self.diag.show()

    def on_next_clicked(self):
        text = line.text()
        # 若为空则返回焦点到输入框
        if ((text != '') and text.isnumeric() and (int(text) > 0) and (int(text)<=15)):
            from sqlite import insert
            insert(jianchahao_text,text,'AGE','string')
            from gender import Gender

            self.diag = Gender(jianchahao_text, text)
            self.diag.show()
            self.window().hide()
        else:
            line.setFocus()
            return 0

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def keyPressEvent(self, event):
        keyEvent = QKeyEvent(event)
        if (keyEvent.key() == Qt.Key_Enter) or (keyEvent.key() == Qt.Key_Return):
            self.on_next_clicked()
    def closeEvent(self, QCloseEvent):
        reply = QMessageBox.question(self, '退出？', "确定退出吗?",
                                     QMessageBox.Yes, QMessageBox.No)
        if reply == QMessageBox.Yes:
            QCloseEvent.accept()
            qApp.quit
        else:
            QCloseEvent.ignore()
